package framework.util.mathCalculate;

import java.util.Date;

/**
 * @author 吴宇亮 on 2019/4/11 0011 下午 12:50
 */
public class _MathUtil {

    public static void main(String[] args) {
        boolean hasIntersection = Range.isHasIntersection(1, 1.5, 2, 5);
        System.out.println(hasIntersection);
    }

    /**
     * 数学中区间的工具类
     */
    public final static class Range {

        private Range(){};

        /**
         * 判断区间(a1, a2)是否合法
         */
        public static void checkIsLegal(double a1, double a2){
            if(a1 > a2){
                throw new IllegalArgumentException("区间的左边为" + a1 + ", 区间的右边为" + a2 + ", 区间的左边要小于等于区间的右边" );
            }
        }

        public static void checkIsLegal(Date a1, Date a2){

        }

        /**
         * 判断两个区间(a1, a2)和(b1, b2)是否有交集<br/>
         * 交集分两种情况<br/>
         * 1.相交 <br/>
         * 2.包含 <br/>
         */
        public static boolean isHasIntersection(double a1, double a2, double b1, double b2){
            checkIsLegal(a1, a2);
            checkIsLegal(b1, b2);
            return isCrossing(a1, a2, b1, b2) || isContain(a1, a2, b1, b2) || isBeInclued(a1, a2, b1, b2);
        }

        /**
         * 判断两个区间(a1, a2)和(b1, b2)是否相交
         */
        public static boolean isCrossing(double a1, double a2, double b1, double b2){
            checkIsLegal(a1, a2);
            checkIsLegal(b1, b2);
            return (a1 >= b1 && a1 <= b2) || (a2 >= b1 && a2 <= b2);
        }

        /**
         * 判断A区间(a1, a2)是否包含B区间(b1, b2)
         */
        public static boolean isContain(double a1, double a2, double b1, double b2){
            checkIsLegal(a1, a2);
            checkIsLegal(b1, b2);
            return a1 <= b1 && a2 >= b2;
        }

        /**
         * 判断A区间(a1, a2)是否被B区间(b1, b2)包含
         */
        public static boolean isBeInclued(double a1, double a2, double b1, double b2){
            if(a1 > a2){
                throw new IllegalArgumentException("区间的左边a1要小于等于区间的右边a2" );
            }else if(b1 > b2){
                throw new IllegalArgumentException("区间的左边b1要小于等于区间的右边b2" );
            }
            return a1 >= b1 && a2 <= b2;
        }
    }


}
